package com.cacheserverdeploy.util;
import java.util.List;

import com.cacheserverdeploy.node.ConsumeNode;
import com.cacheserverdeploy.node.Link;
import com.cacheserverdeploy.node.NetworkNode;

/**
 * 解析输入行
 * Created by Xingfeng on 2017-03-08.
 */
public class Util {

    public static void parseFirstLine(String line) {
        String[] array = line.trim().split(" ");
        Const.NETWORK_NODE_NUM = Integer.parseInt(array[0]);
        Const.NETWORK_LINK_NUM = Integer.parseInt(array[1]);
        Const.CONSUME_NODE_NUM = Integer.parseInt(array[2]);
    }

    public static void parseThridLine(String line) {
    	Const.SERVER_COST = Integer.parseInt(line.trim());
    }

    /**
     * 解析网络链路
     *
     * @param networkNodes 网络节点数组
     * @param graphContent
     * @param start        开始索引，包含
     * @param end          结束索引，不包含
     */
    public static void parseLinks(NetworkNode[] networkNodes, String[] graphContent, int start, int end) {
//  	    networkNodes=null;//清空
        String line = null;
        Link link = null;
        Link link1=null;
        String[] array = null;
        for (int i = start; i < end; i++) {
            line = graphContent[i];
            array = line.trim().split(" ");
            link = new Link(Integer.parseInt(array[0]), Integer.parseInt(array[1])
                    , Integer.parseInt(array[2]), Integer.parseInt(array[3]));
            link1 = new Link(Integer.parseInt(array[1]), Integer.parseInt(array[0])
                    , Integer.parseInt(array[2]), Integer.parseInt(array[3]));           
            networkNodes[link.getFromId()].addLink(link);
            networkNodes[link.getToId()].addLink(link1);
        }

    }

    public static NetworkNode[] initializeNetworkNodes() {

        NetworkNode[] networkNodes = new NetworkNode[Const.NETWORK_NODE_NUM];
        NetworkNode networkNode = null;
        for (int i = 0; i < networkNodes.length; i++) {
            networkNode = new NetworkNode(i);
            networkNodes[i] = networkNode;
        }
        networkNode = null;
        return networkNodes;
    }

    public static ConsumeNode[] parseAndInitializeConsumeNode(NetworkNode[] networkNodes, String[] graphContent, int start, int end) {

        ConsumeNode[] consumeNodes = new ConsumeNode[Const.CONSUME_NODE_NUM];
        String line = null;
        String[] array = null;
        ConsumeNode consumeNode = null;
        for (int i = start; i < end; i++) {
            line = graphContent[i];
            array = line.trim().split(" ");
            consumeNode = new ConsumeNode(Integer.parseInt(array[0]), Integer.parseInt(array[1]), Integer.parseInt(array[2]));
            consumeNodes[i - start] = consumeNode;
            networkNodes[consumeNode.getNetworkNodeId()].setComsumeNodeId(consumeNode.getId());
        }
        return consumeNodes;
    }
    
    /**创建邻接矩阵
     * @return
     */
    public static int[][] constructMatrix( NetworkNode[] networkNodes){
    	int num=networkNodes.length;
    	int[][] mMatrix = new int[num][num];
		for (int i = 0; i < num; i++) {
			for (int j = 0; j < num; j++) {
				mMatrix[i][j] = 100000;//不通的路无穷大
			}
		}
		for (NetworkNode networkNode : networkNodes) {
			List<Link> links=networkNode.getLinks();
			for (Link link : links) {
				//这儿值得商榷
				if (link.getExtraDischarge()>0) {
//					mMatrix[link.getFromId()][link.getToId()]=link.getPrice();
					mMatrix[link.getToId()][link.getFromId()]=link.getPrice();//反过来找的 这儿得注意 不然会超限
				}
				
//				mMatrix[link.getFromId()][link.getToId()]=1;
//				mMatrix[link.getToId()][link.getFromId()]=1;
			}
		}
		
		return mMatrix;
    }
    

    
    
}
